package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;
import com.gc.common.base.Result;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.framework.util.ShiroUtils;
import com.gc.system.domain.SysRole;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.FeedbackMessage;
import com.gc.business.service.IFeedbackMessageService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 意见反馈 控制层
 * 
 * @author hruiyan
 * @date 2020-12-03
 */
@Slf4j
@Controller
@RequestMapping("/business/feedbackMessage")
public class FeedbackMessageController extends BaseController {
    private String prefix = "business/feedbackMessage";
	
	private final IFeedbackMessageService feedbackMessageService;
	private final ISysUserService sysUserService;

	@Autowired
	public FeedbackMessageController(IFeedbackMessageService feedbackMessageService,
									 ISysUserService sysUserService) {
		this.feedbackMessageService = feedbackMessageService;
		this.sysUserService = sysUserService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:feedbackMessage:view")
	@GetMapping
	public String feedbackMessage(){
	    return prefix + "/feedbackMessage";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{feedbackId}")
	public String edit(@PathVariable("feedbackId") Long feedbackId, ModelMap mmap){
		FeedbackMessage feedbackMessage = feedbackMessageService.getById(feedbackId);

		mmap.put("imgList",feedbackMessage.getFeedbackImg().split(","));

		mmap.put("feedbackMessage", feedbackMessage);
		return prefix + "/edit";
	}
	
	/**
	 * 查询意见反馈列表
	 */
	@RequiresPermissions("business:feedbackMessage:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(FeedbackMessage feedbackMessage){

		//查询是否为公司人员
		SysRole sysRole = sysUserService.checkUserRoleByAttrId(ShiroUtils.getSysUser().getAttrId());
		if(StringUtils.isNotNull(sysRole) && sysRole.getRoleKey().equals(BusinessConstants.COMPANY)){
			feedbackMessage.setCompanyId(ShiroUtils.getSysUser().getAttrId());	//查询本公司的意见反馈
		}
		startPage();
        List<FeedbackMessage> list = feedbackMessageService.selectFeedbackList(feedbackMessage);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存意见反馈
	 */
	@RequiresPermissions("business:feedbackMessage:add")
	@Log(title = "意见反馈", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(FeedbackMessage feedbackMessage){
		feedbackMessageService.save(feedbackMessage);
		return Result.success();
	}

	/**
	 * 修改保存意见反馈
	 */
	@RequiresPermissions("business:feedbackMessage:edit")
	@Log(title = "意见反馈", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(FeedbackMessage feedbackMessage){
		feedbackMessageService.updateById(feedbackMessage);
		return Result.success();
	}
	
	/**
	 * 删除意见反馈
	 */
	@RequiresPermissions("business:feedbackMessage:remove")
	@Log(title = "意见反馈", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		feedbackMessageService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出意见反馈列表
	 */
	@RequiresPermissions("business:feedbackMessage:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(FeedbackMessage feedbackMessage){
		List<FeedbackMessage> list = feedbackMessageService.list(feedbackMessage);
		ExcelUtil<FeedbackMessage> util = new ExcelUtil<>(FeedbackMessage.class);
		util.exportExcel(list, "feedbackMessage");
		return Result.success();
	}
}
